﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExtraOpdrachten
{
    public partial class Opdracht12 : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[,] persons = new string[25,4];
                persons[1, 0] = "Glen";
                persons[1, 1] = "";
                persons[1, 2] = "Helder";
                persons[1, 3] = "28-10-1968";
                persons[2, 0] = "Ruud";
                persons[2, 1] = "van";
                persons[2, 2] = "Nistelrooij";
                persons[2, 3] = "01-07-1976";
                persons[3, 0] = "Emile";
                persons[3, 1] = "";
                persons[3, 2] = "Ratelband";
                persons[3, 3] = "11-03-1949";
                persons[4, 0] = "Pierre";
                persons[4, 1] = "van";
                persons[4, 2] = "Hooijdonk";
                persons[4, 3] = "29-11-1969";
                persons[0, 0] = "Sjaak";
                persons[0, 1] = "";
                persons[0, 2] = "Swart";
                persons[0, 3] = "03-07-1938";
                int count = 5;
                Session["persons"] = persons;
                Session["count"] = count;
                for (int i = 1; i < 32; i++)
                {
                    ddlDay.Items.Add(i.ToString());

                }
                for (int i = 1; i < 13; i++)
                {
                    ddlMonth.Items.Add(i.ToString());

                }
                for (int i = DateTime.Now.Year - 100; i <= DateTime.Now.Year ; i++)
                {
                    ddlYear.Items.Add(i.ToString());
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            // Controleer of alle verplichte velden gevuld zijn
            if(txtFirstName.Text.Trim() == string.Empty)
            {
                lblError.Text = "Roepnaam is een verplicht veld";
                txtFirstName.Text = string.Empty;
                txtFirstName.Focus();
                return;
            }
            if (txtLastName.Text.Trim() == string.Empty)
            {
                lblError.Text = "Achternaam is een verplicht veld";
                txtLastName.Text = string.Empty;
                txtLastName.Focus();
                return;
            }
            DateTime geboortedatum;
            if(!DateTime.TryParse(ddlDay.Text + "-" + ddlMonth.Text + "-"
                + ddlYear.Text, out geboortedatum))
            {
                lblError.Text = "Ongeldige datum. Selecteer opnieuw a.u.b.";
                return;
            }
            int count = (int)Session["count"];
            string[,] persons = (string[,])Session["persons"];
            persons[count, 0] = txtFirstName.Text;
            persons[count, 1] = txtMiddleName.Text;
            persons[count, 2] = txtLastName.Text;
            persons[count, 3] = geboortedatum.ToShortDateString();
            count++;
            Session["count"] = count;
            Session["persons"] = persons;
            txtFirstName.Text = string.Empty;
            txtMiddleName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtFirstName.Focus();
            ddlDay.Text = DateTime.Now.Day.ToString();
            ddlMonth.Text = DateTime.Now.Month.ToString();
            ddlYear.Text = DateTime.Now.Year.ToString();
        }

        protected void btnShowResult_Click(object sender, EventArgs e)
        {
            string[,] persons = (string[,])Session["persons"];
            DateTime dateOldest = DateTime.Parse(persons[0,3]);
            DateTime dateYoungest = DateTime.Parse(persons[0, 3]);
            int oldest = 0;
            int youngest = 0;
            for (int i = 1; i < persons.Length / 2; i++)
            {
                DateTime d;
                if (DateTime.TryParse(persons[i, 3], out d))
                {
                    if (d > dateYoungest)
                    {
                        dateYoungest = d;
                        youngest = i;
                    }
                    if (d < dateOldest)
                    {
                        dateOldest = d;
                        oldest = i;
                    }


                }
                else
                {
                    break;
                }
            }
            int ageOldest = DateTime.Now.Year - dateOldest.Year;
            if(DateTime.Now.DayOfYear < dateOldest.DayOfYear)
            {
                ageOldest--;
            }
            int ageYoungest = DateTime.Now.Year - dateYoungest.Year;
            if (DateTime.Now.DayOfYear < dateYoungest.DayOfYear)
            {
                ageYoungest--;
            }

            if (persons[oldest, 1] != "")
            {
                lblResult.Text = "De oudste persoon is " + persons[oldest, 0] +
                    " " + persons[oldest, 1] + " " + persons[oldest, 2] + " en hij is " + ageOldest.ToString() + " jaar oud.<br/>";
            }
            else
            {
                lblResult.Text = "De oudste persoon is " + persons[oldest, 0] +
                    " " + persons[oldest, 2] + " en hij is " + ageOldest.ToString() + " jaar oud.<br/>";
            }
            if (persons[youngest, 1] != "")
            {
                lblResult.Text += "De jongste persoon is " + persons[youngest, 0] +
                    " " + persons[youngest, 1] + " " + persons[youngest, 2] + " en hij is " + ageYoungest.ToString() + " jaar oud.<br/>";
            }
            else
            {
                lblResult.Text += "De oudste persoon is " + persons[youngest, 0] +
                    " " + persons[youngest, 2] + " en hij is " + ageYoungest.ToString() + " jaar oud.<br/>";
            }
        }
    }
}